﻿<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Base.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    UserManager
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <ext:Viewport ID="ViewPort1" runat="server" Layout="fit">
        <Items>
            <ext:GridPanel ID="GridPanel1" runat="server" Header="false" Border="false" TrackMouseOver="true"
                AutoExpandColumn="username" SkipIdForNewRecords="true">
                <Store>
                    <ext:Store ID="dcUser" runat="server" AutoSave="false">
                        <Proxy>
                            <ext:HttpProxy Url="/Account/GetUserStore/" />
                        </Proxy>
                        <UpdateProxy>
                            <ext:HttpWriteProxy Url="/Account/SaveUsesrWithConfirmation/" />
                        </UpdateProxy>
                        <Reader>
                            <ext:JsonReader IDProperty="id" Root="data" TotalProperty="total" MessageProperty="message">
                                <Fields>
                                    <ext:RecordField Name="id" />
                                    <ext:RecordField Name="username" />
                                    <ext:RecordField Name="number" />
                                    <ext:RecordField Name="sex" />
                                    <ext:RecordField Name="lastlogintime" />
                                    <ext:RecordField Name="status" />
                                    <ext:RecordField Name="departmentid" />
                                    <ext:RecordField Name="permissions" />
                                </Fields>
                            </ext:JsonReader>
                        </Reader>
                        <SortInfo Field="username" Direction="DESC" />
                        <Listeners>
                            <Exception Handler="
                                Ext.net.Notification.show({
                                    iconCls   : 'icon-exclamation', 
                                    html      : e && e.message ? e.message : response.message || response.statusText, 
                                    title     : '出错啦', 
                                    hideDelay : 5000
                                });" />
                            <Save Handler="
                                Ext.net.Notification.show({
                                    iconCls   : 'icon-information', 
                                    html      : '保存记录成功', 
                                    title     : '保存成功', 
                                    hideDelay : 5000
                                });" />
                        </Listeners>
                    </ext:Store>
                </Store>
                <ColumnModel ID="ColumnModel1" runat="server">
                    <Columns>
                        <ext:Column ColumnID="ID" DataIndex="ID" Header="ID" Hidden="true">
                            <Editor>
                                <ext:DisplayField ID="DisplayField1" runat="server" StyleSpec="padding-left:5px;font:12px arial, tahoma, helvetica, sans-serif;" />
                            </Editor>
                        </ext:Column>
                        
                        <ext:Column ColumnID="username" DataIndex="username" Header="名称" Width="150">
                            <Editor>
                                <ext:TextField ID="txtUserName" runat="server" AllowBlank="false" SelectOnFocus="true"
                                    ValidateOnBlur="false" EmptyText="请输入用户名">
                                </ext:TextField>
                            </Editor>
                        </ext:Column>
                        <ext:Column ColumnID="usernumber" DataIndex="number" Header="工号" Width="150">
                            <Editor>
                                <ext:TextField ID="TextField1" runat="server" SelectOnFocus="true" AllowBlank="false"
                                    ValidateOnBlur="false" EmptyText="请输入用户工号">
                                    <Listeners>
                                    <Change Fn="afterNumberChange" />
                                    </Listeners>
                                </ext:TextField>
                            </Editor>
                        </ext:Column>
                        <ext:Column ColumnID="sex" DataIndex="sex" Header="性别">
                            <Editor>
                                <ext:SelectBox ID="sltSex" runat="server">
                                    <Items>
                                        <ext:ListItem Value="男" Text="男" />
                                        <ext:ListItem Value="女" Text="女" />
                                    </Items>
                                </ext:SelectBox>
                            </Editor>
                        </ext:Column>
                        <ext:Column ColumnID="lastlogintime" DataIndex="lastlogintime" Header="最近登录时间" Width="150">
                            <%--<Editor>
                                <ext:DisplayField ID="DisplayField2" runat="server" StyleSpec="padding-left:5px;font:12px arial, tahoma, helvetica, sans-serif;" />
                            </Editor>--%>
                        </ext:Column>
                        <ext:Column ColumnID="constructionarea" DataIndex="constructionarea" Header="所属部门"
                            Width="80">
                            <%--<Editor>
                                <ext:DisplayField ID="DisplayField3" runat="server" StyleSpec="padding-left:5px;font:12px arial, tahoma, helvetica, sans-serif;" />
                            </Editor>--%>
                        </ext:Column>
                        <ext:CommandColumn Width="25" Hideable="false">
                            <Commands>
                                <ext:GridCommand CommandName="permissions" Icon="Key">
                                    <ToolTip Text="设置权限" />
                                </ext:GridCommand>
                            </Commands>
                        </ext:CommandColumn>
                        <%--<ext:CommandColumn Width="25" Hideable="false">
                            <Commands>
                                <ext:GridCommand CommandName="delete" Icon="Cross">
                                    <ToolTip Text="删除" />
                                </ext:GridCommand>
                            </Commands>
                        </ext:CommandColumn>--%>
                    </Columns>
                </ColumnModel>
                <TopBar>
                    <ext:Toolbar ID="Toolbar1" runat="server">
                        <Items>
                            <ext:Button ID="btnSave" runat="server" Text="保存" Icon="Disk">
                                <Listeners>
                                    <Click Handler="#{dcUser}.save();" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button ID="btnAdd" runat="server" Text="添加" Icon="Add">
                                <Listeners>
                                    <Click Handler="#{GridPanel1}.insertRecord(0, { name:'',number:'',sex:'',status:1,ID:'',permissions:1});#{GridPanel1}.getView().focusRow(0);#{GridPanel1}.getRowEditor().startEditing(0);" />
                                </Listeners>
                            </ext:Button>
                        </Items>
                    </ext:Toolbar>
                </TopBar>
                <Plugins>
                    <ext:RowEditor ID="RowEditor" runat="server" ErrorSummary="false" ClicksToEdit="1"
                        CancelText="取消" SaveText="确认">
                        <Listeners>
                            <BeforeEdit Fn="setRowEditorState" />
                            <CancelEdit Fn="rowCancelEdit" />
                            <AfterEdit Fn="AfterRowEdit" />
                        </Listeners>
                    </ext:RowEditor>
                </Plugins>
                <SelectionModel>
                    <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" />
                </SelectionModel>
                <LoadMask ShowMask="true" />
                <SaveMask ShowMask="true" />
                <Listeners>
                    <Command Fn="commandHandler" />
                </Listeners>
            </ext:GridPanel>
        </Items>
    </ext:Viewport>
    <ext:Window ID="PermissionsWindow" runat="server" Icon="Key" Width="600"
        Height="400" Hidden="true" Modal="true" Constrain="true">
        <AutoLoad Url="/Day/CheckDetail/" Mode="IFrame" TriggerEvent="show" ReloadOnEvent="true"
            ShowMask="true">
            <Params>
                <ext:Parameter Name="userid" Value="" Mode="Value" />
            </Params>
        </AutoLoad>
        <Buttons>
            <ext:Button ID="Button1" runat="server" Text="关闭">
                <Listeners>
                    <Click Handler="#{PermissionsWindow}.hide();" />
                </Listeners>
            </ext:Button>
        </Buttons>
        <Listeners>
            <%--<Hide Handler="if(this.getBody().customerChanged) { dsCustomers.reload(); };" />--%>
        </Listeners>
    </ext:Window>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeaderArea" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="StartupArea" runat="server">
    <script type="text/javascript">
        var _iscommand = false;

        var AfterRowEdit = function (el, o, r, rowIndex) {

        }

        var setRowEditorState = function (el, rowIndex) {
            if (_iscommand) {
                _iscommand = false;
                return false;
            }
        }

        var rowCancelEdit = function (el, forced) {
            if (GridPanel1.store.data.items[0].data['username'] == "") {
                GridPanel1.store.remove(GridPanel1.store.data.items[0]);
            }
        }

        var commandHandler = function (cmd, record) {
            _iscommand = true;
            switch (cmd) {
                case "delete":
                    Ext.Msg.show({
                        title: '确认',
                        buttons: { yes: '是', no: '否' },
                        msg: '确认删除这个用户吗？',
                        fn: function (btn) {
                            if (btn == "yes") {
                                dcUser.remove(record);
                                dcUser.save();
                            }
                        }
                    });
                    break;
                case "permissions":
                    if (record.data.id == '-1') {
                        Ext.Msg.show({
                            title: 'DailyReport',
                            buttons: { yes: '确认' },
                            msg: '这是新增数据，请先保存然后再修改权限？',
                            fn: function (btn) {
                                return false;
                            }
                        });
                    } else {
                        var win = PermissionsWindow;
                        win.autoLoad.url = '/Account/SetPermissions/';
                        win.autoLoad.params.userid = record.data.id;
                        win.autoLoad.params.permissions = record.data.permissions;
                        win.setTitle([record.data.username, ' 的权限'].join(''));
                        win.show();
                        break;
                    }
            }
        };

        var afterNumberChange = function (item, newValue, oldValue) {
            var odata = GridPanel1.store.data;
            for (i = 0; i < GridPanel1.store.data.items.length; i++) {
                if (GridPanel1.store.data.items[i].data['number'] == newValue) {
                    item.setValue(oldValue);
                    Ext.Msg.show({
                        title: 'DailyReport',
                        buttons: { yes: '确认' },
                        msg: '列表中已包含相同的工号。',
                        fn: function (btn) {
                            return false;
                        }
                    });

                }
            }
        }

        Ext.onReady(function () {

        });
    </script>
</asp:Content>
